// importClass(org.xml.sax)

runtime.loadDex('libs/jsoup.dex')
importClass(org.jsoup.Jsoup)
// importClass(android.widget.TextView)

let StringTool = require("./module/StringTool"),
St = new StringTool()



let BanHua = (function () {

  function BanHua (obj) {
    this.name = 'BanHua'
    for (o in obj) {
      this[o] = obj[o]
    }
    this.newEl = obj['el']
  }

  BanHua.prototype.init = function () {
    //开始渲染数据
    this.__randerData__()

    //开始绑定数据
    this.__bindEvent__()
    //开始渲染视图测试
    ui.layout(this.newEl)
  }

  BanHua.prototype.__randerData__ = function () {
    // print("-->>  "+newEl.toString())
    if (!this.el) throw "请传入el参数(xml布局)"

    //开始处理xml
    // let bh_view_datas = this.newEl.toString().match(/\-.*?\-/g)
    let bh_view_datas = St.jq(this.newEl.toString(), "-", "-")
    

    bh_view_datas.forEach((v, index) => {
      
      this.newEl = this.newEl.toString().replace(v, this.data[v.replace(/\-/g, "")])
    })
  }

  BanHua.prototype.__bindEvent__ = function () {
    if (!this.el) throw "请传入el参数(xml布局)"

    //开始处理xml
    bh_view_datas = St.jq(this.newEl.toString(), "on_", "@")
    
    let document = Jsoup.parse(this.newEl, "UTF-8")
    let x =  document.getElementsByAttribute('on_click')
    for (let i =0;i < x.size(); i++) {
      let vid = x.get(i).id()
      let property = x.get(i).attr("on_click")
      print(property)
      //循环添加监听事件
      ui.post(() => {
        ui[vid].on("click", this.computed[property])

      }, 0)
    }

    //查找表单
    let formView = document.getElementById("form")

    if (formView) {
      let temp = {}
      for (f in this.form) {
        let className = formView.getElementById(f).tagName()
        temp[f] = {
          cn : className,
          value : this.form[f]
        }
      }
      
      //添加表单内容监听
      ui.post(() => {
        for (t in temp) {
          let tt
          switch (temp[t].cn) {
            case "input":
              tt = t
              print('设置文本内容%s，id为%s', temp[t].value, t)
              
              ui[t].setText(temp[t].value)
              ui[t].addTextChangedListener(new android.text.TextWatcher({
                afterTextChanged : (v)=> {
                  this.form[tt] = v
                }

              }))
            break;
            case "radiogroup":
              tt = t
              print('开始设置radiogroup!')
              for (radio in temp[tt].value) {
                // print(radio)
                ui[radio].setChecked(temp[tt].value[radio])
              }
              
              //监听radiogroup
              ui[tt].setOnCheckedChangeListener(new android.widget.RadioGroup.OnCheckedChangeListener({
                onCheckedChanged: (g, i) => {
                  let v = ui.findView(i)
                  // print(i.id())
                  for (let ii in v) {
                    // print(ii)
                  }
                  // print(v.getId())
                  this.form[tt].value = v.getText()
                  // print(this.form)
                }
              }))

              //监听radio
              print('开始监听radio')
              print(temp[tt])
              for (let rv in temp[tt].value) {
                print(rv)
                
              }

            break;
            case "checkbox":
              ui[t].setChecked(temp[t].value)
            break;
          }
        }
      }, 0)

      
    }
    
    
  }

  return BanHua
})()

module.exports = BanHua